﻿// <copyright file="CaseInsensitiveFind.cs" company="Sinclair Community College">
// Copyright (c) Sinclair Community College. All rights reserved.
// </copyright>

namespace SinclairCC.AppVSuite.DuplicateRootDetector
{
    using System;

    /// <summary>
    /// This class defines a method which allows a case-insensitive search of a List of strings.
    /// </summary>
    internal class CaseInsensitiveFind
    {
        /// <summary>
        /// The text to be found in the list.
        /// </summary>
        private string textToFind;

        /// <summary>
        /// Initializes a new instance of the CaseInsensitiveFind class.
        /// </summary>
        /// <param name="text">
        /// The text to be found among the list items.
        /// </param>
        public CaseInsensitiveFind(string text)
        {
            this.textToFind = text;
        }

        /// <summary>
        /// Gets or sets the text to be found among the list items.
        /// </summary>
        public string Text
        {
            get { return this.textToFind; }
            set { this.textToFind = value; }
        }

        /// <summary>
        /// Gets a value indicating whether the given string matches a defined condition.
        /// </summary>
        /// <remarks>
        /// In this case, the condition is a case-insensitive comparison of the entire string.
        /// </remarks>
        public Predicate<string> Match
        {
            get { return this.IsMatch; }
        }

        /// <summary>
        /// Determines whether the given string is a case-insensitive match for this object's target value.
        /// </summary>
        /// <param name="compareToValue">
        /// The string to be compared with this object's target value.
        /// </param>
        /// <returns>
        /// Returns true if the two strings are a case-insensitive match. Otherwise, returns false.
        /// </returns>
        private bool IsMatch(string compareToValue)
        {
            return string.Compare(this.textToFind, compareToValue, StringComparison.InvariantCultureIgnoreCase) == 0;
        }
    }
}
